# ①导包
import time

import pytest

from utils import DriverUitls, get_el_text, read_json, cunzaiys
from page.login_page import HomePage
import allure

class TestLogin:

    # 类级别前置方法：打开浏览器
    def setup_class(self):
        # ②创建浏览器驱动对象
        self.driver = DriverUitls.get_driver()
        self.driver.maximize_window()

    # 类级别后置方法：关闭浏览器
    def teardown_class(self):
        # ⑥浏览器驱动对象退出
        DriverUitls.quit_driver()

    # 方法级别前置方法：重新打开页面
    def setup_method(self):
        # ③访问页面
        self.driver.get("https://www.ypshuo.com/")

    # 方法级别后置方法：每个用例执行完成之后，停留2S
    def teardown_method(self):
        # ⑤暂停几秒钟
        time.sleep(2)

    #登陆功能测试
    @pytest.mark.parametrize("case_name,user,pwd,exp_el,expect",
                             read_json("login_shuji"))
    def test01_login_suc(self,case_name,user,pwd,exp_el,expect):
        allure.dynamic.title(case_name)
        allure.dynamic.severity("normal")
        with allure.step("测试步骤一:在首页点击登陆按钮，进入登陆页面"):
            HomePage().home_to_login()
        with allure.step("测试步骤二:在登陆页面输入相关信息，点击登陆按钮完成登陆"):
            HomePage().login(user,pwd)
        with allure.step("测试步骤三:断言"):
            msg = get_el_text(self.driver, exp_el, expect)
            assert msg == expect
        allure.attach(self.driver.get_screenshot_as_png(),
                      "登陆失败截图",allure.attachment_type.PNG)

    @allure.title("验证登陆成功")
    @allure.severity(allure.severity_level.BLOCKER)
    def test02_login_suc(self):
        with allure.step("测试步骤一:在首页点击登陆按钮，进入登陆页面"):
            HomePage().home_to_login()
        with allure.step("测试步骤二:在登陆页面输入相关信息，点击登陆按钮完成登陆"):
            HomePage().login("1697551573@qq.com","123123123")
        with allure.step("测试步骤三:断言"):
            is_suc = cunzaiys(self.driver,"//*[@id='app']/header/div/div[2]")
            assert is_suc == True
        allure.attach(self.driver.get_screenshot_as_png(),
                      "登陆成功截图",allure.attachment_type.PNG)
